package com.mbbb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.mbbb.constant.MessageConstant;
import com.mbbb.pojo.TravelItem;
import com.mbbb.service.TravelItemService;
import com.mbbb.vo.PageResult;
import com.mbbb.vo.QueryPageBean;
import com.mbbb.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/TravelItem")//请求映射父路径，一般设置为实体类的类名
public class TravelItemController {

    @Reference
    TravelItemService travelItemService;

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<TravelItem> list = travelItemService.findAll();
            return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            travelItemService.delete(id);
            return new Result(true,MessageConstant.DELETE_TRAVELITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELITEM_FAIL);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody TravelItem travelItem){//@RequestBody：从请求体中获取json对象，并转化为Bean对象
        try {
            travelItemService.update(travelItem);
            return new Result(true, MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/get")
    public Result get(Integer id){
        try {
            TravelItem travelItem = travelItemService.get(id);
            return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = travelItemService.findPage(
                                   queryPageBean.getCurrentPage(),
                                   queryPageBean.getPageSize(),
                                   queryPageBean.getQueryString());
        return pageResult;
    }

    @PostMapping("/add")
    public Result add(@RequestBody TravelItem travelItem){//@RequestBody：从请求体中获取json对象，并转化为Bean对象
        try {
            travelItemService.add(travelItem);
            return new Result(true, MessageConstant.ADD_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TRAVELITEM_FAIL);
        }
    }
}
